/**
 * 
 */
package mx.frantor.catalogos.daoimp;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import mx.frantor.catalogos.dao.OrdendeCompraMateriaPrimaDAO;
import mx.frantor.catalogos.entitys.OrdendecompramateriaprimaEntity;
import mx.frantor.util.HibernateUtil;

/**
 * @author Hp
 *
 */
public class OrdenDeCompraMateriaPrimaDAOImpl implements
		OrdendeCompraMateriaPrimaDAO {

	/* (non-Javadoc)
	 * @see mx.frantor.catalogos.dao.OrdendeCompraMateriaPrimaDAO#agregarOrdenCMP(mx.frantor.catalogos.entitys.OrdendecompramateriaprimaEntity)
	 */
	@Override
	public String agregarOrdenCMP(OrdendecompramateriaprimaEntity nuevoOrdenCMP) {
		// TODO Auto-generated method stub
		String e = "";
		Session session = HibernateUtil.getSessionFactory()
				.getCurrentSession();
		
		session.beginTransaction();
		try {
		session.save(nuevoOrdenCMP);
		session.flush();
		session.getTransaction().commit();
		e = nuevoOrdenCMP.getIdRelacion().toString();
		
		} catch (Exception x) {
			//e.printStackTrace();
			session.getTransaction().rollback();
			e = x.getMessage();
			//return e.toString();
		}
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		return e;
	}

	/* (non-Javadoc)
	 * @see mx.frantor.catalogos.dao.OrdendeCompraMateriaPrimaDAO#modificaOrdenCMP(mx.frantor.catalogos.entitys.OrdendecompramateriaprimaEntity)
	 */
	@Override
	public String modificaOrdenCMP(
			OrdendecompramateriaprimaEntity modificarOrdenCMP) {
		// TODO Auto-generated method stub
		String e = "";
		Session session = HibernateUtil.getSessionFactory()
				.getCurrentSession();
	
		session.beginTransaction();
		try {
			OrdendecompramateriaprimaEntity ordenCMPTemp = (OrdendecompramateriaprimaEntity) session.get(
					OrdendecompramateriaprimaEntity.class, modificarOrdenCMP.getIdRelacion());
		session.flush();
		session.evict(ordenCMPTemp);
		session.update(modificarOrdenCMP);
		session.getTransaction().commit();
		
		} catch (Exception x) {
			//e.printStackTrace();
			session.getTransaction().rollback();
			e = x.getMessage();
			//return e.toString();
		}
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		return e;
	}

	/* (non-Javadoc)
	 * @see mx.frantor.catalogos.dao.OrdendeCompraMateriaPrimaDAO#eliminarOrdenCMP(java.lang.Integer)
	 */
	@Override
	public String eliminarOrdenCMP(Long idOrdenCMP) {
		// TODO Auto-generated method stub
		String e = "";
		Session session = HibernateUtil.getSessionFactory()
				.getCurrentSession();
		

			// TODO Auto-generated method stub			
			session.beginTransaction();
			try {
				OrdendecompramateriaprimaEntity ordenCMPElimina = (OrdendecompramateriaprimaEntity) session.get(
						OrdendecompramateriaprimaEntity.class, idOrdenCMP);
			session.delete(ordenCMPElimina);
			session.getTransaction().commit();
		} catch (Exception x) {
			//e.printStackTrace();
			session.getTransaction().rollback();
			e = x.getMessage();
			//return e.toString();
		}
		//HibernateUtil.getSessionFactory().getCurrentSession().close();
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		return e;
	}

	/* (non-Javadoc)
	 * @see mx.frantor.catalogos.dao.OrdendeCompraMateriaPrimaDAO#getTodosOrdenCMPActivos()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<OrdendecompramateriaprimaEntity> getTodosOrdenCMPActivos() {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<OrdendecompramateriaprimaEntity> ordenmatp = session.createCriteria(OrdendecompramateriaprimaEntity.class)
				.addOrder(Order.asc("IdRelacion")).list();
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		return ordenmatp;
	}

	/* (non-Javadoc)
	 * @see mx.frantor.catalogos.dao.OrdendeCompraMateriaPrimaDAO#getOrdenCMPId(java.lang.Integer)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public OrdendecompramateriaprimaEntity getOrdenCMPId(Long idOrdenCMP) {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<OrdendecompramateriaprimaEntity> ordencmp = (List<OrdendecompramateriaprimaEntity>) session.createCriteria(OrdendecompramateriaprimaEntity.class)
				.add(Restrictions.eq("IdRelacion", idOrdenCMP))
				.setFetchSize(1).list();
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		if(ordencmp==null){
			return null;
		}else{
			return ordencmp.get(0);
		}	
		}

	/* (non-Javadoc)
	 * @see mx.frantor.catalogos.dao.OrdendeCompraMateriaPrimaDAO#getOrdenCMPPorId(java.lang.Integer)
	 */
	@SuppressWarnings("unchecked")
	public List<OrdendecompramateriaprimaEntity> getOrdenCMPPorId(
			Long idOrdenCMP) {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<OrdendecompramateriaprimaEntity> ordencmp = (List<OrdendecompramateriaprimaEntity>) session.createCriteria(OrdendecompramateriaprimaEntity.class)
				.add(Restrictions.eq("IdOrdenDeCompra", idOrdenCMP))
				.list();
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		return ordencmp;
	
	}

}
